package work.display.effect
{
	/**
	 * 
	 * @author 张峰
	 * @QQ:37520626
	 * @Email:7677@163.com
	 * @Date:2013-1-7 下午3:44:14
	 * 
	 */
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import gs.TweenLite;
	import gs.easing.*;
	/**
	 * 动画执行完毕事件 
	 * @eventType flash.events.Event.COMPLETE
	 */
	[Bindable]
	[Event(name="complete", type="flash.events.Event")]
	public class TurnSprite extends Sprite
	{
		private var asp:Sprite
		private var bsp:Sprite;
		
		private var clip:Sprite;
		
		private var tw:Number;
		private var th:Number;
		
		private var a:DisplayObject;
		private var b:DisplayObject;
		
		private var _isTween:Boolean=false;
		/**
		 * 翻转执行时间 
		 */
		public static var tweenTime:Number=1;
		/**
		 * 翻转执行缓动函数 
		 */
		public static var tweenEase:Function=Back.easeOut;	
		
		public var data:Object;
		
		/**
		 * 翻转精灵 
		 * @param w 宽
		 * @param h 高
		 * @param defaultDisplayObject 默认显示对象
		 * 
		 */
		public function TurnSprite(w:Number,h:Number,defaultDisplayObject:DisplayObject=null)
		{
			super();
			clip=new Sprite();
			clip.x=w*0.5;
			clip.y=h*0.5;
			this.addChild(clip);
			
			tw=w;
			th=h;
			
			asp=new Sprite();
			bsp=new Sprite();
			
			bsp.visible=false;
			
			if(defaultDisplayObject)
			{
				a=createDisplayObject(defaultDisplayObject);
				asp.addChild(a);
			}
			clip.addChild(asp);
			clip.addChild(bsp);
		}
		/**
		 * 是否正在缓动翻转 
		 * @return 
		 * 
		 */
		public function get isTween():Boolean
		{
			return _isTween;
		}
		private function createDisplayObject(dp:DisplayObject):DisplayObject
		{
			dp.width=tw;
			dp.height=th;
			
			dp.x=-dp.width*0.5;
			dp.y=-dp.height*0.5;
			
			return dp;
		}
		private function initVariable():void
		{
			
		}
		
		private function removeBDP():void
		{
			if(b)
			{
				b.parent.removeChild(b);
			}
			b=null;
		}
		/**
		 * 沿X轴正方向翻转 
		 * @param newDisplayObject
		 * 
		 */
		public function turnX(newDisplayObject:DisplayObject):void
		{
			if(isTween)
			{
				return;
			}
			_isTween=true;
			removeBDP();
			b=createDisplayObject(newDisplayObject);
			bsp.addChild(b);
			bsp.rotationX=180;
			
			TweenLite.to(clip,tweenTime,{rotationX:180,ease:tweenEase,onUpdate:turnXTweenUpdate,onComplete:turnXTweenComplete});
		}
		/**
		 * 沿X轴负方向翻转 
		 * @param newDisplayObject
		 * 
		 */
		public function turnOBX(newDisplayObject:DisplayObject):void
		{
			if(isTween)
			{
				return;
			}
			_isTween=true;
			removeBDP();
			b=createDisplayObject(newDisplayObject);
			bsp.addChild(b);
			bsp.rotationX=180;
			
			TweenLite.to(clip,tweenTime,{rotationX:-180,ease:tweenEase,onUpdate:turnOBXTweenUpdate,onComplete:turnXTweenComplete});
		}
		
		private function turnXTweenUpdate():void
		{
			if(clip.rotationX>90)
			{
				asp.visible=false;
				bsp.visible=true;
			}
		}
		private function turnOBXTweenUpdate():void
		{
			if(clip.rotationX<-90)
			{
				asp.visible=false;
				bsp.visible=true;
			}
		}
		private function turnXTweenComplete():void
		{
			clip.rotationX=0;
			
			asp.rotationX=0;
			bsp.rotationX=0;
			
			var sp:Sprite=asp;
			asp=bsp;
			bsp=sp;
			
			var dp:DisplayObject=a;
			a=b;
			b=dp;
			
			_isTween=false;
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		/**
		 * 沿Y轴正方向翻转 
		 * @param newDisplayObject
		 * 
		 */
		public function turnY(newDisplayObject:DisplayObject):void
		{
			if(isTween)
			{
				return;
			}
			_isTween=true;
			removeBDP();
			b=createDisplayObject(newDisplayObject);
			bsp.addChild(b);
			bsp.rotationY=180;
			
			TweenLite.to(clip,1,{rotationY:180,ease:tweenEase,onUpdate:turnYTweenUpdate,onComplete:turnYTweenComplete});
		}
		/**
		 * 沿Y轴负方向翻转 
		 * @param newDisplayObject
		 * 
		 */
		public function turnOBY(newDisplayObject:DisplayObject):void
		{
			if(isTween)
			{
				return;
			}
			_isTween=true;
			removeBDP();
			b=createDisplayObject(newDisplayObject);
			bsp.addChild(b);
			bsp.rotationY=-180;
			
			TweenLite.to(clip,1,{rotationY:-180,ease:tweenEase,onUpdate:turnOBYTweenUpdate,onComplete:turnYTweenComplete});
		}
		
		private function turnYTweenUpdate():void
		{
			if(clip.rotationY>90)
			{
				asp.visible=false;
				bsp.visible=true;
			}
		}
		private function turnOBYTweenUpdate():void
		{
			if(clip.rotationY<-90)
			{
				asp.visible=false;
				bsp.visible=true;
			}
		}
		private function turnYTweenComplete():void
		{
			clip.rotationY=0;
			
			asp.rotationY=0;
			bsp.rotationY=0;
			
			var sp:Sprite=asp;
			asp=bsp;
			bsp=sp;
			
			var dp:DisplayObject=a;
			a=b;
			b=dp;
			
			_isTween=false;
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}